package au.com.mason.propertyTracker.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import au.com.mason.propertyTracker.domain.Property;
import au.com.mason.propertyTracker.domain.Suburb;
import au.com.mason.propertyTracker.dto.SearchParameters;
import au.com.mason.propertyTracker.form.DisplayPropertiesForm;
import au.com.mason.propertyTracker.service.PropertyService;
import au.com.mason.propertyTracker.service.SuburbService;

@Controller
public class DisplayProperties {
	
	private PropertyService propertyService;
	private SuburbService suburbService;
	
	@Autowired
	public DisplayProperties(PropertyService propertyService, SuburbService suburbService) {
		this.propertyService = propertyService;
		this.suburbService = suburbService;
	}
	
	@RequestMapping("/SubmitFilterProperties.action")
	public String submitFilterProperties(Map model, DisplayPropertiesForm displayPropertiesForm) {
		process(displayPropertiesForm, model);
		
		return "displayProperties";
	}
	
	@RequestMapping("/SubmitFilterPropertiesFromSession.action")
	public String submitFilterPropertiesFromSession(HttpSession session, Map model) {
		DisplayPropertiesForm displayPropertiesForm = new DisplayPropertiesForm();
		SearchParameters searchParameters = (SearchParameters) session.getAttribute("searchParameters");
		displayPropertiesForm.setSearchParameters(searchParameters);
		
		process(displayPropertiesForm, model);
		
		if (session.getAttribute("messages") != null) {
			model.put("messages", session.getAttribute("messages"));
			session.setAttribute("messages", null);
		}
		
		return "displayProperties";
	}

	private void process(DisplayPropertiesForm displayPropertiesForm, Map model) {
		Suburb suburb = null;
		if (displayPropertiesForm.getSearchParameters() == null) displayPropertiesForm.setSearchParameters(new SearchParameters());
		
		if (displayPropertiesForm.getSearchParameters().getSuburbId() != null) {
			suburb = suburbService.getSuburb(displayPropertiesForm.getSearchParameters().getSuburbId());
		}
		if (displayPropertiesForm.getSearchParameters().getPage() == null || displayPropertiesForm.getSearchParameters().getPage() == 0) {
			displayPropertiesForm.getSearchParameters().setPage(1);
		}
		List<Property> fullList = propertyService.filterProperties(suburb, displayPropertiesForm.getSearchParameters());
		
		displayPropertiesForm.getSearchParameters().setNextAndPrevious(fullList.size());
		
		List<Property> properties = new ArrayList<Property>();
		int count = 0;
		for (int i = 1; i < displayPropertiesForm.getSearchParameters().getPage(); i++) {
			count += SearchParameters.PAGE_SIZE;
		}
		for (int j = count; j < count + SearchParameters.PAGE_SIZE && j < fullList.size(); j++) {
			properties.add(fullList.get(j));
		}
		
		model.put("properties", properties);
		model.put("suburbs", suburbService.getSuburbs());
		model.put("displayPropertiesForm", displayPropertiesForm);
	}
	
	@RequestMapping("/SaveSearchResults.action")
	public String saveSearchResults(DisplayPropertiesForm displayPropertiesForm, HttpSession session) {
		session.setAttribute("searchParameters", displayPropertiesForm.getSearchParameters());
		
		
		if (displayPropertiesForm.getRedirect().equals("addRentalProperty")) {
			session.setAttribute("rental", Boolean.TRUE);
		}
		else if (displayPropertiesForm.getRedirect().equals("editProperty") || displayPropertiesForm.getRedirect().equals("deleteProperty")) {
			session.setAttribute("propertyId", displayPropertiesForm.getPropertyId());
		}
		
		
		return "redirect:" + getRedirect(displayPropertiesForm.getRedirect());
	}
	
	private String getRedirect(String redirect) {
		if (redirect.equals("addProperty")) return "displayAddProperty.action";
		else if (redirect.equals("addRentalProperty")) return "displayAddProperty.action";
		else if (redirect.equals("referenceData")) return "DisplayReferenceData.action";
		else if (redirect.equals("findUnderContractProperties")) return "FindUnderContractProperties.action";
		else if (redirect.equals("autoAddProperty")) return "DisplayAutoAddProperty.action";
		else if (redirect.equals("editProperty")) return "displayEditProperty.action";
		else if (redirect.equals("deleteProperty")) return "SubmitDeleteProperty.action";
		else return "ShowReport.action";
	}
	
}
